package cn.enited.devtools.apiaggregator.api.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Objects;
import java.util.stream.Collectors;

public class TripletHashMap<K, V1, V2> extends HashMap<K, Tuple<V1, V2>> implements TripletMap<K, V1, V2> {
    private static final long serialVersionUID = 7722919946664299350L;

    public Collection<V1> firstValues() {
        return this.values().parallelStream().map(Tuple::getItem1).collect(Collectors.toList());
    }

    public Collection<V2> secondValues() {
        return this.values().parallelStream().map(Tuple::getItem2).collect(Collectors.toList());
    }

    public Tuple<V1, V2> put(K key, V1 firstValue, V2 secondValue) {
        return this.put(key, new Tuple<>(firstValue, secondValue));
    }

    public Tuple<V1, V2> putIfAbsent(K key, V1 firstValue, V2 secondValue) {
        return this.putIfAbsent(key, new Tuple<>(firstValue, secondValue));
    }

    public V1 getValue1(K key) {
        Tuple<V1, V2> value = this.get(key);
        return Objects.nonNull(value) ? value.getItem1() : null;
    }

    public V1 getValue1OrDefault(K key, V1 other) {
        V1 v1 = getValue1(key);
        return Objects.nonNull(v1) ? v1 : other;
    }

    public V2 getValue2(K key) {
        Tuple<V1, V2> value = this.get(key);
        return Objects.nonNull(value) ? value.getItem2() : null;
    }

    public V2 getValue2OrDefault(K key, V2 other) {
        V2 v2 = getValue2(key);
        return Objects.nonNull(v2) ? v2 : other;
    }
}
